home *** CD-ROM | disk | FTP | other *** search
- //: C06:Stack3.cpp {O}
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
- // Constructors/destructors
- #include "Stack3.h"
- #include "../require.h"
- using namespace std;
-
- Stack::Link::Link(void* dat, Link* nxt) {
- data = dat;
- next = nxt;
- }
-
- Stack::Link::~Link() {
- delete data;
- }
-
- Stack::Stack() { head = 0; }
-
- void Stack::push(void* dat) {
- head = new Link(dat,head);
- }
-
- void* Stack::peek() { return head->data; }
-
- void* Stack::pop() {
- if(head == 0) return 0;
- void* result = head->data;
- Link* oldHead = head;
- head = head->next;
- delete oldHead;
- return result;
- }
-
- Stack::~Stack() {
- Link* cursor = head;
- while(head) {
- cursor = cursor->next;
- delete head;
- head = cursor;
- }
- head = 0; // Officially empty
- } ///:~
-